package structure;

public class MyQueue {
    public static class ListNode {
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode front;
    public ListNode last;
    public int usedSize = 0;

    public void offer(int e) {
        ListNode node = new ListNode(e);
        if (front == null) {
            front = node;
        } else {
            last.next = node;
            node.prev = last;
        }
        last = node;
        usedSize++;
    }

    public void display() {
        System.out.print("[");
        ListNode cur = front;
        while (cur != null) {
            System.out.print(cur.val);
            if (cur.next != null) {
                System.out.print(", ");
            }
            cur = cur.next;
        }
        System.out.print("]");
    }

    public int poll() {
        int value = 0;
        if (front == null) {
            return -1;
        } else if (front == last) {
            last = null;
            front = null;
        } else {
            value = front.val;
            front = front.next;
            front.prev = null;
            --usedSize;
        }
        return value;
    }
    public int peek(){
        if(front == null){
            return -1;
        }
        return front.val;
    }
    public int size(){
        return usedSize;
    }
    public boolean isEmpty(){
        return front == null;
    }
}


